package org.example.iris.controller;

import org.example.iris.entity.Job;
import org.example.iris.dto.JobInfo;
import org.example.iris.dto.Result;
import org.example.iris.service.JobService;
import org.example.iris.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    JobService jobService;

    @GetMapping("/recommend")
    public Result<List<JobInfo>> recommend() {

        List<JobInfo> recommendJob = jobService.recommendJob();

        return Result.success(recommendJob);
    }

    /**
     * 发布岗位
     * @param companyId 企业ID
     * @param title 岗位名称
     * @param description 岗位描述
     * @param salary 岗位薪水
     * @param requirement 岗位要求
     * @param publishDate 发布日期
     * @param status 岗位状态（"published" 或 "closed"）
     * @return 发布结果
     */
    @PostMapping("/publish")
    public String publishJob(
            @RequestParam("companyId") Integer companyId,
            @RequestParam("title") String title,
            @RequestParam("description") String description,
            @RequestParam("salary") String salary,
            @RequestParam("requirement") String requirement,
            @RequestParam("publishDate") @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date publishDate,  // 使用 java.util.Date
            @RequestParam("status") String status,
            @RequestParam("area") String area ) {

        // 将 java.util.Date 转换为 java.sql.Date
        java.sql.Date sqlPublishDate = new java.sql.Date(publishDate.getTime());

        // 创建 Job 对象
        Job job = new Job();
        job.setCompanyId(companyId);
        job.setTitle(title);
        job.setDescription(description);
        job.setSalary(salary);
        job.setRequirement(requirement);
        job.setPublishDate(sqlPublishDate);  // 设置 sql.Date
        job.setStatus(status);
        job.setArea(area);

        // 调用 JobService 发布岗位
        boolean isPublished = jobService.publishJob(job);

        // 返回发布结果
        if (isPublished) {
            return "岗位发布成功";
        } else {
            return "岗位发布失败";
        }
    }
    @PostMapping("/selectJob")
    public Result<List<JobInfo>> selectJob(@RequestBody Map<String, String> params) {
        List<JobInfo> jobs = jobService.selectJob(params.get("keywords"),params.get("city"));
        return Result.success(jobs);
    }

    @PutMapping("applyJob")
    public Result applyJob(Integer jobId, Integer fileId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        jobService.applyJob(userId, jobId, fileId);
        return Result.success();
    }

    @GetMapping("/jobInfoById")
    public Result<JobInfo> getJobInfo(@RequestParam Integer jobId) {
        JobInfo jobInfo = jobService.getJobInfoById(jobId);
        return Result.success(jobInfo);
    }
}
